
import vue from '@vitejs/plugin-vue'
import { defineConfig,loadEnv } from "vite"
// 下面是自动引入的插件
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'

import {resolve} from 'path'
import autoprefixer from 'autoprefixer'
import postcssFlexbugsFixes from 'postcss-flexbugs-fixes'
import postcssPresetEnv from "postcss-preset-env"
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import fs from "fs"

// 根据工作文件夹的开发环境让vite加载开发配置文件，拿到开发配置文件中创建的常量
const env=loadEnv("development",process.cwd(),"")
// console.log(env,"<<<=== env");



const {VITE_APP_BASE_API}=env

console.log(VITE_APP_BASE_API,"<<<=== VITE_APP_BASE_API 接口方法的公共路径")

export default defineConfig({
    server:{
        cors:true,
        host: true,
        /*https: {
            key: fs.readFileSync('./certs/localhost+5-key.pem'),
            cert: fs.readFileSync('./certs/localhost+5.pem')
        },*/
        port: 2323,
        proxy:{
           "/dev-api":{//通过[变量]动态设置键名，变量的值必须是字符串
                // target: "http://192.168.3.4/prod-api",
                target: "http://pk-dev.com:8080",
                changeOrigin:true,
                rewrite:(path)=>{
                    return path.replace(/\/dev-api/,"")
                }
            }
        }
    },
    resolve:{
        alias:[
            {
                find:'@',//设置src文件夹的别名
                replacement:resolve(__dirname,'./src')
            },
            {
                find:"~",//设置根文件夹的别名
                replacement:resolve(__dirname,"./")
            }
        ]
    },
    css:{
        devSourcemap:true,
        modules:{
            scopeBehaviour:"local"
        },
        postcss:{
            plugins:[
                postcssPresetEnv(),
                postcssFlexbugsFixes(),
                autoprefixer({
                    overrideBrowserslist:[
                        "> 1%",
                        "last 2 versions"
                    ],
                }),
                {
                    postcssPlugin:"internal:charset-removal",//删除css文件中的字符集，不删会影响css处理
                    AtRule:{
                        charset:(atRule)=>{
                            if(atRule.name==="charset"){
                                atRule.remove()
                            }
                        }
                    }
                }
            ],
        },
        preprocessorOptions:{
            scss:{
                additionalData:`@import "@/common.scss";`//全局都能使用scss样式
            }

        }
    },
    plugins:[
        createSvgIconsPlugin({
            iconDirs:[resolve(process.cwd(),'src/assets/svg')],
            symbolId: 'icon-[dir]-[name]',
        }),
        vue(),//vite对vue的支持
        AutoImport({
            resolvers: [
                ElementPlusResolver(),
                IconsResolver({
                    prefix:"Icon"
                })
            ],
        }),
        Components({
            resolvers: [
                ElementPlusResolver(),
                IconsResolver({
                    enabledCollections: ['ep'],
                })
            ],
          }),
        Icons({
            autoInstall:true
        })
    ],
    optimizeDeps:{
        exclude:[]
    }

})
